package com.hl.toolkit.framework.core.util.common;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/** 
* @author vic 获取mapper中的语句
* @date 2017年8月30日 下午4:56:15 
* @describe 
*/

public class MapperUtil {
	
	/**
	 * mapper文件存放路径
	 */
	private final String MAPPER_FILE_PATH = "E:/new_project/Project_SVN/Code/anpotech-admin/src/main/resources/mapper/";	
	private Map<String, String> mapperXmlList = new HashMap<String, String>();	//所有的mapper文件内容列表
	private static MapperUtil mapperUtil;
	
	public String sql = "";
	
	public MapperUtil()
	{
		this.loadAllMapperXml();
	}
	
	/**
	 * 
	 * @return
	 */
	public static MapperUtil instance()
	{
		if (null == mapperUtil)
		{
			mapperUtil = new MapperUtil();
		}
		
		return mapperUtil;
	}
	
	/**
	 * 加载所有的mapper文件内容
	 */
	private void loadAllMapperXml()
	{
		// 1. 获取所有的xml文件名
		Map<String, String> xmlFile = XmlUtil.getAllXmlFile(MAPPER_FILE_PATH);
		Iterator<Entry<String, String>> iterator = xmlFile.entrySet().iterator();  
		  
		while (iterator.hasNext()) 
		{  
			  try
			  {
				  	Entry<String, String> entry = iterator.next();  
				  	String content = entry.getValue();
				    Document document = DocumentHelper.parseText(content);
					Element root = document.getRootElement();
					String namespace = root.attributeValue("namespace");
					
				    mapperXmlList.put(namespace,  content);

			  }
			  catch (Exception e)
			  {
				  e.printStackTrace();
			  }
			    
		  
		}  
	}
	
	/**
	 * 根据id 获取sql内容
	 * @param id
	 * @return
	 */
	public String getSqlNodeStr(String namespace, String id)
	{
		String result = "";
		try 
		{	
			String xmlContent = mapperXmlList.get(namespace);
			if(xmlContent!=null&&!xmlContent.isEmpty())
			{
				Document document = DocumentHelper.parseText(xmlContent);
				
				Element element = document.elementByID(id);
				String content = element.asXML(); 
				result = content.substring(content.indexOf(">") + 1, content.lastIndexOf("<"));
				
				result=result.replaceAll("\"", "'");
			}

		} 
		catch (DocumentException e) 
		{
			e.printStackTrace();
		}
		
		sql = result;
		
		return result;
	}
	
	/**
	 * 获取mapper.xml中的MyBatis语言
	 * @param fileName 文件名称
	 * @param type	   方法类型 例如 update insert 
	 * @param methdId 语句id
	 * @param string 
	 * @return
	 */
	public String getMapperStr(String path,String fileName, String type,String methdId){
		SAXReader reader = new SAXReader();
		 String result=new String();
		 try {
			 path="E:/new_project/Project_SVN/Code/anpotech-admin/src/main/resources/mapper/";
			// 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
			Document document = reader.read(new File(path+fileName+".xml"));
			Element bookStore = document.getRootElement();
			Node root = document.selectSingleNode("/mapper");

			List list = root.selectNodes(type+"[@id='"+methdId+"']");
			for (Object object : list) {
				 Element e = (Element) object;
				 String testxml = e.asXML();
				 result = testxml.substring(testxml.indexOf(">") + 1, testxml.lastIndexOf("<"));
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return result;
		
	}
}
 